package database.viewer;

import grouper.*;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JFrame;

import database.Database;
import database.table.DBTableData;

public class DBTableViewer extends JFrame implements ActionListener {
	private Database db;
	private TableListPanel	tableList;
	private TablePanel		table;
	private InfoPanel		info;
	private CommandPanel	command;
	
	private Grouper			grouper;
	
	public static final int		WIDTH	= 1000;
	public static final int		HEIGHT 	= 600;
	
	public static final String	COMMAND_LOADTABLE		= "loadtable";
	public static final String	COMMAND_LOADTABLEALL	= "loadtableall";
	public static final String	COMMAND_GROUPELEMENTS	= "groupelements";
	public static final String	COMMAND_PROPAGATE		= "propagate";
	public static final String	COMMAND_TABLECHANGE		= "tablechange";
	public static final String	COMMAND_ELEMENTCHANGE	= "elementchange";
	public static final String	COMMAND_COMMIT			= "commit";
	
	public DBTableViewer( Database db, String names[] ) {
		super("Table Viewer");
		this.db = db;
		
		setPreferredSize(new Dimension(WIDTH, HEIGHT));
		setDefaultCloseOperation(EXIT_ON_CLOSE);		
		setLayout(new GridBagLayout());
		
		GridBagConstraints constraints = new GridBagConstraints();
		constraints.fill = GridBagConstraints.BOTH;
		
		tableList = new TableListPanel(this);
		constraints.gridx = 0;
		constraints.gridy = 0;
		constraints.gridheight = 2;
		constraints.weightx = 1;
		constraints.weighty = 1.0;
		add(tableList, constraints);
		
		table = new TablePanel(this);
		
		constraints.gridx = 1;
		constraints.gridy = 0;
		constraints.gridheight = 2;
		constraints.weightx = 5;
		constraints.weighty = 1.0;
		add(table, constraints);
		
		info = new InfoPanel(this);
		info.addInfo("ID", null);
		info.addInfo("Page", null);
		info.addInfo("Position", null);
		info.addInfo("Size", null);
		
		constraints.gridx = 2;
		constraints.gridy = 0;
		constraints.gridheight = 1;
		constraints.weightx = 0.3;
		constraints.weighty = 1.0;
		add(info, constraints);
		
		command = new CommandPanel(this);
		//getRootPane().setDefaultButton(defaultButton);
		constraints.gridx = 2;
		constraints.gridy = 1;
		constraints.gridheight = 1;
		constraints.weightx = 1.5;
		constraints.weighty = 0.0;
		add(command, constraints);
		
		setVisible(true);
		pack();
	}
	
	public void loadTable( String name ) {
		DBTableData data;
		int row;
		
		if (!tableList.isTableLoaded(name)) {
			try {
				data = new DBTableData(name, db);
				row = table.addTableData(data);
				table.selectTableData(row);
				tableList.assignTableData(name, data);
			}
			catch (SQLException e) {
				System.out.println("ERROR: unable to load table");
				System.out.println(e.getMessage());
			}
		}
		else {
			System.out.println("ERROR: table already loaded");
		}
	}
	
	// ----------------------------------
	
	public void loadSelectedTable() {
		String name = tableList.getSelectedTable();
		
		if (name != null)
			loadTable(name);
	}
	
	public void loadAllTables() {
		for (int i = 0; i < tableList.getTableCount(); i++)
			loadTable( tableList.getTable(i));
	}
	
	public void groupElements() {
		grouper = new SimpleGrouper(table.getSelectedTableData(), 0.1);
		grouper.group();
	}
	
	public void propagate() {
		grouper = new SimpleGrouper(table.getSelectedTableData(), 0.1);
		grouper.propagateAll("chars");
	}
	
	public void selectTable() {
		table.selectTableData(tableList.getSelectedTable());
	}
	
	public void selectElement() {
		DBTableData data = table.getSelectedTableData();
		int row = table.getSelectedRow();
		
		if (data == null || row == -1) 
			return;
		
		info.setInfo("ID", data.getValueAt(row, "id"));
		info.setInfo("Page", data.getValueAt(row, "page_no"));
		info.setInfo("Position", data.getValueAt(row, "x") + ", " + data.getValueAt(row, "y"));
		info.setInfo("Size", data.getValueAt(row, "width") + ", " + data.getValueAt(row, "height"));
		info.setImage(data.getBlobStreamAt(row, "image"));
		
		//info.update( table.getSelectedRow(), table.getSelectedTableData() );
	}
	
	public void commit() {
		DBTableData data = table.getSelectedTableData();
		
		if (data != null)
			data.commit("chars");
	}
	
	// ----------------------
	
	@Override
	public void actionPerformed(ActionEvent e) {
		String cmd = e.getActionCommand();

		if (cmd.equals(COMMAND_LOADTABLE)) {
			loadSelectedTable();
		} else if (cmd.equals(COMMAND_LOADTABLEALL)) {
			loadAllTables();
		} else if (cmd.equals(COMMAND_GROUPELEMENTS)) {
			groupElements();
		} else if (cmd.equals(COMMAND_PROPAGATE)) {
			propagate();
		} else if (cmd.equals(COMMAND_TABLECHANGE)) {
			selectTable();
		} else if (cmd.equals(COMMAND_ELEMENTCHANGE)) {
			selectElement();
		} else if (cmd.equals(COMMAND_COMMIT)) {
			commit();
		}
	}

}
